package com.itcncp.common.util;

import com.itcncp.common.model.OrderCodeType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class OrderUtils {

	private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
	public static String getOrderNo(String mobile) {
		
		StringBuilder orderNo = new StringBuilder();
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssS");
		String dtString = formatter.format(date);
		if(mobile.length()>11){
			mobile = mobile.substring(7,mobile.length());
		}else{
			Integer imobile =(int)(Math.random()*9000)+1000;
			
			mobile = imobile.toString().substring(0,4);
		}
		Integer i =(int)(Math.random()*900)+100;
		
		String randomNumber = i.toString().substring(0,3);
		
		orderNo.append(dtString).append(mobile).append(randomNumber);
		
		return orderNo.toString();
	}
	
	public static Long createOrderId(OrderCodeType codeType) {

		StringBuilder orderNo = new StringBuilder();
		StringBuilder msg = new StringBuilder();
		//加上订单前缀
		orderNo.append(codeType.getHeadCode());
//		msg.append("订单前缀"+codeType.getHeadCode()+"："+codeType.getHeadCode().length()+"位");

		//加时间戳
		String dtString = String.format("%015d",Long.parseLong(DateUtils.dateFormatNow("yyMMddHHmmssS")));
		orderNo.append(dtString);
//		msg.append("======时间戳"+dtString+"："+dtString.length()+"位");

		//8位UDID
//		int hashCodeV = MD5Util.UUID().hashCode();
//		if(hashCodeV < 0) {//有可能是负数
//			hashCodeV = - hashCodeV;
//		}
//		String hashCode = String.format("%08d", hashCodeV);
//		orderNo.append(hashCode);
//		msg.append("======UDID"+hashCode+"："+hashCode.length()+"位");

		//二位随机数
		Integer i =(int)(Math.random()*99)+10;
		String randomNumber = i.toString().substring(0,2);
		orderNo.append(randomNumber);
//		msg.append("======随机数"+randomNumber+"："+randomNumber.length()+"位");
//		System.out.print("订单号生成规则,共"+orderNo.length()+"位"+msg);

		return Long.parseLong(orderNo.toString());
	}

	/**
	 * <p>
	 * 根据编码类型获取最新编码
	 * </p>
	 *
	 * @param codeType 类型枚举
	 * @return 最新编码
	 */
//	public static String createOrderId(OrderCodeType codeType){
//		StringBuffer codeBuff=new StringBuffer();
//		codeBuff.append(codeType.getHeadCode());
//		int num=codeBuff.length();
//		int codeNum=1;
//
//		int machineId = 1;//最大支持1-9个集群机器部署
//		int hashCodeV = MD5Util.UUID().hashCode();
//		if(hashCodeV < 0) {//有可能是负数
//			hashCodeV = - hashCodeV;
//		}
//
//		String FORMAT = codeType.getHeadCode()+(10-num)+hashCodeV;
//
//		FORMAT = String.format("%015d", Long.parseLong(FORMAT));
//		System.out.print( FORMAT+ " | ");
//
//		// 0 代表前面补充0
//		// 4 代表长度为4
//		// d 代表参数为正数型
//		return machineId+String.format("%015d", hashCodeV);
//	}


	public static void main(String[] args) {
		String ss =createOrderId( OrderCodeType.CASHIN_NO).toString();
		System.out.println("======");
		System.out.println( ss + " | " +ss.length());
	}

}
